﻿using System;
using System.Data;
using System.ComponentModel;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using System.Windows.Forms;
using System.Linq;
using Mota.EditModule;
using System.Collections.Generic;

namespace Mota.XMLControls
{

    /// <summary>
    /// Represent List based on DX ListBoxControl
    /// </summary>
    public class DXGuiListBox:GuiListBox
    {
        /// <summary>
        /// Create ListBoxControl 
        /// and set its CheckOnClick property to true
        /// </summary>
        public DXGuiListBox()
        {
            visualControl = new ListBoxControl();
            (visualControl as ListBoxControl).SelectedValueChanged += DXGuiListBox_SelectedValueChanged;
        }

        void DXGuiListBox_SelectedValueChanged(object sender, EventArgs e)
        {
            this.ChangeEventCall(null);
        }

        /// <summary>
        /// GET|SET ListBoxControl Text property
        /// </summary>
        public override string Text
        {
            get
            {
                return (visualControl as CheckedListBoxControl).Text;
            }
            set
            {
                (visualControl as CheckedListBoxControl).Text = value;
            }
        }

        /// <summary>
        /// SET| GET ListBoxControl DataSource property
        /// </summary>
        public override object Binding
        {
            get
            {
                return (visualControl as ListBoxControl).DataSource;
            }
            set
            {
                if (value is BindingSource)
                {
                    var vc = visualControl as ListBoxControl;

                    vc.BeginUpdate();
                    try
                    {
                        vc.DataSource = value;
                        vc.ValueMember = XmlAttributes["_KeyField"];
                        vc.DisplayMember = XmlAttributes["_Fields"].Split(',').Last();
                    }
                    finally
                    {
                        vc.EndUpdate();
                    }
                }
            }
        }
        /// <summary>
        /// Set Control to current main BindingSource value
        /// </summary>
        /// <param name="valueBinding">main BindingSource with current record values</param>
        public override void SetValue(object valueBinding)
        {
            if ((valueBinding is BindingSource) && ((visualControl as ListBoxControl).DataBindings["SelectedValue"] == null))
                (visualControl as ListBoxControl).DataBindings.Add("SelectedValue", valueBinding as BindingSource, XmlAttributes["XMLAttribute"], true);
        }


        public override void Refresh(ConnectionConfigs configs, BindingSource mainBs)
        {
            this.Rfsh(mainControls, mainBs, configs);
        }

        public override object GetValue()
        {
            return (visualControl as ListBoxControl).SelectedValue;
        }
    }
}
    